import unittest
import ddt

# https://blog.csdn.net/u013118036/article/details/45558951
from excel_read import ExcelUtil
import requests

excel = ExcelUtil(r'.\data.xlsx', 'Sheet1')


@ddt.ddt
class DataTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print('start')

    @classmethod
    def tearDownClass(cls):
        print('stop')

    @ddt.data(*excel.next())
    def testLogin(self, data):
        # 建立连接对象
        self.s = requests.Session()
        # 获取接口测试即将要用到的参数
        url = data['url']
        username = data['username']
        pwd = data['password']
        expect_result = data['expected result']  # 预期结果

        # 构造正文
        data = 'username=%s&password=%s&savelogin=true' % (username, pwd)
        # 构造头部
        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        # 发送登陆请求
        r = self.s.post(url, data=data, headers=headers)
        # 获取响应, 即实际结果
        resp = r.text
        # 断言
        self.assertEqual(resp, expect_result)
        # 关闭连接
        self.s.close()


if __name__ == "__main__":
    suite = unittest.TestLoader().loadTestsFromTestCase(DataTest)
    unittest.TextTestRunner(verbosity=2).run(suite)
    # unittestdemo.main()
